#include "stm32f10x.h"                  // Device header

uint8_t Serial_Tx_Data[2];
uint8_t Serial_Rx_Data[2];
uint8_t Serial_Rx_flag;
void Serial_Init(void)
{
	//时钟初始化
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
	//IO初始化
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_PinRemapConfig( GPIO_Remap_USART1, ENABLE);
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_Init( GPIOA, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init( GPIOB, &GPIO_InitStructure);
	
	//USART初始化配置
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init( USART1, &USART_InitStructure);
	
	//中断初始化
	USART_ITConfig( USART1, USART_IT_RXNE, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init( &NVIC_InitStructure);
	
	USART_Cmd( USART1, ENABLE);	
}

/*
**发送一个字节的数据
*/
void Serial_SendData(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while( USART_GetFlagStatus( USART1, USART_FLAG_TXE) == RESET);
	
}

/*
**发送长度为2的数组
*/
void Serial_SendArray(uint8_t * Array)
{
	USART_SendData(USART1, Array[0]);
	while( USART_GetFlagStatus( USART1, USART_FLAG_TXE) == RESET);
	USART_SendData(USART1, Array[1]);
	while( USART_GetFlagStatus( USART1, USART_FLAG_TXE) == RESET);
}

/*
**发送包含两个数据的数据包
*/
void Serial_SendPacket(void)
{
	Serial_SendData(0xFF);
	Serial_SendArray(Serial_Tx_Data);
	Serial_SendData(0xFE);
}
/*
**获取Rx标志位状态并使Rx标志位清零
*/
uint8_t Serial_GetRxFlag(void)
{
	if ( Serial_Rx_flag == 1)
	{
		Serial_Rx_flag = 0;
		return 1;
	}
	return 0;
}

/*
**USART1中断函数
**负责接收串口数据
*/
//void USART1_IRQHandler(void)
//{
//	Serial_Tx_Data[0] = 99;
//	static uint8_t RxState = 0;
//	static uint8_t CountRx = 0;
//	if( USART_GetFlagStatus(USART1, USART_FLAG_RXNE == SET))
//	{
//		uint8_t RxData = USART_ReceiveData(USART1);
//		//使用状态机接收数据
//		if( RxState == 0) //数据包包头
//		{
//			if(RxData == 0xFF)
//			{
//				RxState = 1;
//				CountRx = 0;
//			}
//		}
//		else if( RxState == 1) //数据帧
//		{
//			Serial_Rx_Data[CountRx] = RxData;
//			CountRx ++;
//			if( CountRx >=2) RxState = 2;
//		}
//		else if( RxState == 2) //数据包包尾
//		{
//			if( RxData == 0xFE)
//			{
//				RxState = 0;
//				Serial_Rx_flag = 1;
//			}
//		}
//		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//	}
//}
void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;
	static uint8_t pRxPacket = 0;
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		uint8_t RxData = USART_ReceiveData(USART1);
		
		if (RxState == 0)
		{
			if (RxData == 0xFF)
			{
				RxState = 1;
				pRxPacket = 0;
			}
		}
		else if (RxState == 1)
		{
			Serial_Rx_Data[pRxPacket] = RxData;
			pRxPacket ++;
			if (pRxPacket >= 2)
			{
				RxState = 2;
			}
		}
		else if (RxState == 2)
		{
			if (RxData == 0xFE)
			{
				RxState = 0;
				Serial_Rx_flag= 1;
			}
		}
		
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}

